package com.neo.pub.utils;

import com.syt.service.SendMsgDetileServiceDelegate;
import com.syt.service.SendMsgDetileServiceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class SMSUtils {

    public static final Integer MSG_LIMIT = 210;

    @Value("${sms.username}")
    private String userName;
    @Value("${sms.passWord}")
    private String passWord;
    @Value("${sms.isSend}")
    private boolean isSend;

    @Autowired
    private SendMsgDetileServiceService sendMsgDetileServiceService;

    //发送短信
    public void sendMsg(List<String> phoneList, String str) {
        phoneList.forEach(phone -> {
            if (str.length() - 9 > MSG_LIMIT) {
                List<String> stringList = this.splitString(str);
                for (int i = 0; i < stringList.size(); i++) {
//                        System.out.println("【第" + (i + 1) + "/" + stringList.size() + "条】" + stringList.get(i));
                    this.send(phone, "第" + (i + 1) + "/" + stringList.size() + "条：" + stringList.get(i));
                }
            } else if (str.length() <= MSG_LIMIT) {
//                    System.out.println(sb.toString());
                this.send(phone, str);
            }
        });
    }

    public boolean send(String phone, String msg) {
        boolean flag = true;
        try {
            SendMsgDetileServiceDelegate smdsd = sendMsgDetileServiceService.getSendMsgDetileServicePort();
            if(!isSend){
                System.out.println("进入短信发送接口");
//                flag = smdsd.saveSendMsgDetile(userName, passWord, phone, msg);
            }
            if(flag){
                log.info("短信发送成功：" + phone + "," + msg);
            }
        } catch (Exception e) {
            log.error("短信发送失败：" + phone + "--" + msg + "---" + e.getMessage());
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }


    //处理短信内容
    private List<String> splitString(String msg) {
        List<String> strList = new ArrayList<>();
        int len = (int) Math.ceil(msg.length() / MSG_LIMIT);
        for (int i = 0; i < len + 1; i++) {
//            System.out.println(msg);
            if (msg.length() == 0) {
                return strList;
            }
            String str = msg.substring(0, msg.length() < MSG_LIMIT ? msg.length() : MSG_LIMIT);
            int lastIndex1 = str.lastIndexOf(",");
            int lastIndex2 = str.lastIndexOf(";");
            int lastIndex = lastIndex1 > lastIndex2 ? lastIndex1 : lastIndex2;
//            int lastIndex = str.lastIndexOf(";");
            msg = msg.substring(lastIndex + 1, msg.length());
            strList.add(str.substring(0, lastIndex + 1));
        }
//        for (String s : strList) {
//            System.out.println(s);
//        }
        return strList;
    }

    public static Integer getMsgLimit() {
        return MSG_LIMIT;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public SendMsgDetileServiceService getSendMsgDetileServiceService() {
        return sendMsgDetileServiceService;
    }

    public void setSendMsgDetileServiceService(SendMsgDetileServiceService sendMsgDetileServiceService) {
        this.sendMsgDetileServiceService = sendMsgDetileServiceService;
    }
}
